package com.macro.mall.tiny.controller;

import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.mbg.model.PmsProductAttributeCategory;
import com.macro.mall.tiny.service.PmsProductAttributeCategoryService;
import com.macro.mall.tiny.vo.ProductAttributeCategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/productAttribute/category")
@Api(tags="productAttributeCategory")
@Tag(name="productAttributeCategory",description = "商品分类属性管理")
@PreAuthorize("@ex.hasAuthority('/productAttribute/category')")
public class PmsProductAttributeCategoryController {

    @Autowired
    private PmsProductAttributeCategoryService pmsProductAttributeCategoryService;

    //添加商品属性分类
    @PostMapping("/create")
    @ApiOperation("添加商品属性分类")
//    @ResponseBody
    public CommonResult create(@RequestParam(value="name") String name){
        log.info("添加商品属性分类:{}",name);

        Integer code=pmsProductAttributeCategoryService.create(name);
        return code==1? CommonResult.success() : CommonResult.failed();
    }

    //修改商品属性分类
    @PostMapping("update/{id}")
    @ApiOperation("修改商品属性分类")
//    @ResponseBody
    public CommonResult update(
            @RequestParam(value="name") String name,
            @PathVariable Long id){
        log.info("修改商品属性分类：{}{}",name,id);

        Integer code=pmsProductAttributeCategoryService.update(name,id);
        return code==1? CommonResult.success():CommonResult.failed();

    }

    //删除单个属性分类
    @GetMapping("delete/{id}")
    @ApiOperation("删除单个属性分类")
//    @ResponseBody
    public CommonResult deleteById(@PathVariable Long id){
        log.info("删除单个属性分类：{}",id);

        Integer code=pmsProductAttributeCategoryService.deleteById(id);
        return code==1? CommonResult.success():CommonResult.failed();
    }

    //获取单个属性分类信息
    @GetMapping("/{id}")
    @ApiOperation("获取单个属性分类信息")
//    @ResponseBody
    public CommonResult<PmsProductAttributeCategory> getById(@PathVariable Long id){
        log.info("获取单个属性分类信息:{}",id);

        PmsProductAttributeCategory pmsProductAttributeCategory=pmsProductAttributeCategoryService.getById(id);
        return CommonResult.success(pmsProductAttributeCategory);

    }

    //分页获取所有商品属性分类
    @GetMapping("/list")
    @ApiOperation("分页获取所有商品属性分类")
//    @ResponseBody
    public CommonResult<CommonPage<PmsProductAttributeCategory>> getByIdList(
            @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value="pageSize",defaultValue = "5") Integer pageSize){
        log.info("分页获取所有商品属性分类;{},{}",pageNum,pageSize);

        List<PmsProductAttributeCategory> pmsProductAttributeCategoryList=pmsProductAttributeCategoryService.getByIdList(pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(pmsProductAttributeCategoryList));
    }

//    获取所有商品属性分类及其下属性
    @GetMapping("/list/withAttr")
    @ApiOperation("获取所有商品属性分类及其下属性")
//    @ResponseBody
    public CommonResult<List<ProductAttributeCategoryVO>> getListWithAttr(){
        log.info("获取所有商品属性分类及其下属性");

        List<ProductAttributeCategoryVO> productAttributeCategoryVOList=pmsProductAttributeCategoryService.getListWithAttr();
        return CommonResult.success(productAttributeCategoryVOList);
    }


}
